package day01;

import java.sql.*;

public class TestJDBC2 {


    private static String driverClassName="com.mysql.jdbc.Driver";
    private static String url="jdbc:mysql://127.0.0.1:3306/jdbc?useUnicode=true&characterEncoding=utf8";
    private static String username="root";
    private static String password="123";


    public static void main(String[] args) {
//        insert();

//        update();

//        delete();

//        select();


//        selectById(5);

//        selectByName("ddd");

        selectByName("sadfsadfasdf or 1=1");
    }





    public static void insert(){
        Connection con=null;
        PreparedStatement ps=null;
        try{
            Class.forName(driverClassName);

            con=DriverManager.getConnection(url,username,password);

            String sql=new StringBuffer()
                    .append("insert into t_user ")
                    .append("    (name) ")
                    .append("values ")
                    .append("    (?) ")
                    .toString();

            ps=con.prepareStatement(sql);


            ps.setString(1,"eee");

            ps.executeUpdate();


        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if(ps!=null){
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(con!=null){
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void update(){
        Connection con=null;
        PreparedStatement ps=null;
        try{
            Class.forName(driverClassName);

            con=DriverManager.getConnection(url,username,password);

            String sql=new StringBuffer()
                    .append("update t_user ")
                    .append("set name=? ")
                    .append("where id=? ")
                    .toString();

            ps=con.prepareStatement(sql);

            ps.setString(1,"abc");
            ps.setInt(2,3);

            ps.executeUpdate();


        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if(ps!=null){
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(con!=null){
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void delete(){
        Connection con=null;
        PreparedStatement ps=null;
        try{
            Class.forName(driverClassName);

            con=DriverManager.getConnection(url,username,password);

            String sql=new StringBuffer()
                    .append("delete from t_user ")
                    .append("where id=? ")
                    .toString();

            ps=con.prepareStatement(sql);

            ps.setInt(1,3);

            ps.executeUpdate();


        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if(ps!=null){
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(con!=null){
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void select(){
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            Class.forName(driverClassName);

            con=DriverManager.getConnection(url,username,password);

            String sql=new StringBuffer()
                    .append("select * ")
                    .append("from t_user ")
                    .toString();

            ps=con.prepareStatement(sql);


            rs=ps.executeQuery();

            while (rs.next()) {
                System.out.println(rs.getInt("id")+"\t"+rs.getString("name"));
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if(rs!=null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(ps!=null){
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(con!=null){
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void selectById(int id){
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            Class.forName(driverClassName);

            con=DriverManager.getConnection(url,username,password);

            String sql=new StringBuffer()
                    .append("select * ")
                    .append("from t_user ")
                    .append("where id=? ")
                    .toString();

            ps=con.prepareStatement(sql);

            ps.setInt(1,id);

            rs=ps.executeQuery();

            if (rs.next()) {
                System.out.println(rs.getInt("id")+"\t"+rs.getString("name"));
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if(rs!=null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(ps!=null){
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(con!=null){
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void selectByName(String name){
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            Class.forName(driverClassName);

            con=DriverManager.getConnection(url,username,password);

            String sql=new StringBuffer()
                    .append("select * ")
                    .append("from t_user ")
                    .append("where name=? ")
                    .toString();

            ps=con.prepareStatement(sql);

            ps.setString(1,name);

            rs=ps.executeQuery();

            while(rs.next()){
                System.out.println(rs.getInt("id")+"\t"+rs.getString("name"));
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if(rs!=null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(ps!=null){
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(con!=null){
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }































}
